import java.util.*;

/**
 * @author LKQ
 * @date 2022/7/11 9:22
 * @description hashMap存储相同长度的字符串列表，节约时间
 */
public class MagicDictionary {
    public static void main(String[] args) {

    }
    private static Map<Integer, List<String>> dic;
    public MagicDictionary() {
        dic = new HashMap<>();
    }

    public void buildDict(String[] dictionary) {
        for(String d : dictionary) {
            dic.computeIfAbsent(d.length(), (unused) -> new ArrayList<>()).add(d);
        }
    }

    public boolean search(String searchWord) {
        List<String> list = dic.get(searchWord.length());
        if (list == null) {
            return false;
        }
        for (String s : list) {
            if (isLackOne(s, searchWord)) {
                return true;
            }
        }
        return false;
    }
    public boolean isLackOne(String a, String b) {
        int cnt = 0;
        for (int i = 0; i < a.length(); i++) {
            if (a.charAt(i) != b.charAt(i)) {
                cnt++;
            }
            if (cnt > 1) {
                return false;
            }
        }
        return cnt == 1;
    }
}
